/**

 * @Description:TODO

 * @author:liyang

 * @time:2017年7月31日 下午8:46:51

 */
package com.alex.insistence.web.serverlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author:liyang
 * @time:2017年7月31日 下午8:46:51
 * @since:1.0
 */
@SuppressWarnings("serial")
public class ImageShowServlet extends HttpServlet {

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String filePath = request.getParameter("filePath");
    OutputStream os = response.getOutputStream();
    File file = new File(filePath);
    FileInputStream fips = new FileInputStream(file);
    byte[] btImg = readStream(fips);
    os.write(btImg);
    os.flush();
  }

  /**
   * 读取管道中的流数据
   */
  public byte[] readStream(InputStream inStream) {
    ByteArrayOutputStream bops = new ByteArrayOutputStream();
    int data = -1;
    try {
      while ((data = inStream.read()) != -1) {
        bops.write(data);
      }
      return bops.toByteArray();
    } catch (Exception e) {
      return null;
    }
  }
}